package middle

import (
	"day12_10_homework/conterlles"
	"day12_10_homework/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AuthMiddle() gin.HandlerFunc {
	return func(c *gin.Context) {
		header := c.GetHeader("token")
		if header == "" {
			c.JSON(http.StatusOK, conterlles.JsonRes{
				Code:    500,
				Message: "暂未登录",
				Data:    nil,
			})
			return
		}
		token, s := pkg.GetToken(header)
		if s != "" {
			c.JSON(http.StatusOK, conterlles.JsonRes{
				Code:    500,
				Message: s,
				Data:    nil,
			})
			return
		}
		s2 := token["user"].(string)
		c.Set("userId", s2)
		c.Next()
	}
}
